namespace Xsxiaosa.Net
{
    using System;
//jyj add
	using System.Threading;

    internal class FtpUploadStream : FtpStream
    {
        // Methods
        public FtpUploadStream(Ftp ftp, bool disconnectOnClose, string remotePath) : base(ftp, disconnectOnClose)
        {
            this._buffer = null;
            this._offset = 0;
            this._count = 0;
            this._ftp.BeginPutFile(this, remotePath, new AsyncCallback(this.Finish), null);
            base.WaitForTransfer(FtpTransferState.Uploading);
        }

        public override void Close()
        {
            object obj1 = this._sync;
            lock (obj1)
            {
                if (this._closed)
                {
                    return;
                }
                this._closed = true;
            }
            base.ResetTimeout();
        Label_0030:
            obj1 = this._sync;
            lock (obj1)
            {
                if (!this._finished)
                {
                    goto Label_0050;
                }
                goto Label_0093;
            }
        Label_0050:
            if ((this._timeout > 0) && ((((long) Environment.TickCount) - this._timerStart) > ((long) this._timeout)))
            {
                throw new FtpException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "Timeout"), FtpExceptionStatus.Timeout);
            }
            Thread.Sleep(1);
            goto Label_0030;
        Label_0093:
            if (this._disconnectOnClose)
            {
                this._ftp.Disconnect();
                this._ftp.Dispose();
            }
            GC.SuppressFinalize(this);
            if (this._exception == null)
            {
                return;
            }
            if ((this._exception as FtpException) != null)
            {
                throw new FtpException(((FtpException) this._exception));
            }
            throw new FtpException(this._exception.Message, this._exception);
        }

        private void Finish(IAsyncResult ar)
        {
            try
            {
                this._ftp.EndPutFile(ar);
            }
            catch (Exception exception1)
            {
                this._exception = exception1;
            }
            object obj1 = this._sync;
            lock (obj1)
            {
                this._finished = true;
                base.TransferStarted = true;
            }
        }

        public override int ReadInternal(byte[] buffer, int offset, int count)
        {
            object obj1;
        Label_0000:
            obj1 = this._sync;
            lock (obj1)
            {
                if (this._closed)
                {
                    return 0;
                }
                if (this._count <= 0)
                {
                    goto Label_0030;
                }
                goto Label_0038;
            }
        Label_0030:
            Thread.Sleep(1);
            goto Label_0000;
        Label_0038:
            obj1 = this._sync;
            lock (obj1)
            {
                if (count > this._count)
                {
                    count = this._count;
                }
                Array.Copy(this._buffer, this._offset, buffer, offset, count);
                this._offset += count;
                this._count -= count;
                if (this._count != 0)
                {
                    goto Label_009E;
                }
                this._buffer = null;
            }
        Label_009E:
            return count;
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer", Xsxiaosa.Net.Messages.GetString("ArgumentNullBuffer"));
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }
            if ((buffer.Length - offset) < count)
            {
                throw new ArgumentException(Xsxiaosa.Net.Messages.GetString("ArgumentInvalidOffset"));
            }
            object obj1 = this._sync;
            lock (obj1)
            {
                if (this._closed)
                {
                    throw new InvalidOperationException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "StreamClosed"));
                }
                if (this._finished)
                {
                    base.ThrowException();
                }
                if (count == 0)
                {
                    return;
                }
                this._buffer = new byte[((uint) count)];
                Array.Copy(buffer, offset, this._buffer, 0, count);
                this._count = count;
                this._offset = 0;
            }
            base.ResetTimeout();
        Label_00C9:
            obj1 = this._sync;
            lock (obj1)
            {
                if (this._count == 0)
                {
                    return;
                }
                if (!this._finished)
                {
                    goto Label_00F7;
                }
                base.ThrowException();
            }
        Label_00F7:
            base.CheckTimeout();
            Thread.Sleep(1);
            goto Label_00C9;
        }


        // Properties
        public override bool CanWrite
        {
            get
            {
                return true;
            }
        }


        // Fields
        private byte[] _buffer;
        private int _count;
        private int _offset;
    }}

